package org.me.rmi.Stub;

import org.me.rmi.Interface.IPerson;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.net.Socket;

/**
 * Created by wangyg on 2016/11/1.
 */
public class PersonStub implements IPerson {
	private Socket socket;

	public PersonStub() throws Throwable {
		// connect to skeleton
		socket = new Socket("localhost", 9000);
//		socket.setSoTimeout(200);
	}

	public int getAge() throws Throwable {
		// pass method name to skeleton
		ObjectOutputStream outStream =
				new ObjectOutputStream(socket.getOutputStream());
		outStream.writeObject("age");
		outStream.flush();
		ObjectInputStream inStream =
				new ObjectInputStream(socket.getInputStream());
		return inStream.readInt();
	}

	public String getName() throws Throwable {
		// pass method name to skeleton
		ObjectOutputStream outStream =
				new ObjectOutputStream(socket.getOutputStream());
		outStream.writeObject("name");
		outStream.flush();
		ObjectInputStream inStream =
				new ObjectInputStream(socket.getInputStream());

		return (String) inStream.readObject();
	}
//	public void closeSocket() throws IOException {
//		socket.shutdownInput();
//	}
}

